package com.iot.inter.controller.newinterface.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.subResponse.DeviceBatchRealTimeResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.BatchDeviceRealTime;
import com.iot.inter.entity.newpojo.response.subResponse.common.BatchDeviceRealTimePackageOrder;
import com.iot.inter.entity.newpojo.response.subResponse.common.BatchDeviceRealTimeRefuelingPackageList;
import com.iot.inter.entity.newpojo.response.subResponse.common.Page;
import com.iot.inter.entity.newrequest.device.batchdevicerealtime.request.RequestBatchDeviceRealTime;
import com.iot.inter.entity.newrequest.device.batchdevicerealtime.response.ResponseBatchDeviceOrderRefuelingPackage;
import com.iot.inter.entity.newrequest.device.batchdevicerealtime.response.ResponseBatchDevicePackageOrder;
import com.iot.inter.entity.newrequest.device.batchdevicerealtime.response.ResponseBatchDeviceRealTime;
import com.iot.inter.entity.newrequest.device.batchdevicerealtime.response.ResponseBatchDeviceTradeData;
import com.iot.inter.interfaceprocessing.newinterface.TurnToDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.2	批量获取拥有的设备信息：2.0接口
 *
 * @author lm
 * @date 2019/8/26
 */
@RestController
@Api(description = "3.2批量获取拥有的设备信息")
public class BatchDeviceQueryRealTimeController {
    private static Logger logger = Logger.getLogger(BatchDeviceQueryRealTimeController.class);

    @CrossOrigin
    @RequestMapping(value = "batchDeviceRealTime", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.2批量获取拥有的设备信息", response = ResponseBatchDeviceRealTime.class)
    public String queryDeviceRealTime(@ApiParam @RequestBody RequestBatchDeviceRealTime request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【批量获取拥有的设备信息】请求：" + requestStr);

            RequestBatchDeviceRealTime requestPackage = JSONArray.parseObject(requestStr, RequestBatchDeviceRealTime.class);
            Integer pageNo = requestPackage.getPageNo();
            Integer pageSize = requestPackage.getPageSize();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            //3.2批量获取拥有的设备信息
            TurnToDeviceService realTimeService = new TurnToDeviceService();

            DeviceBatchRealTimeResponse response = realTimeService.batchDeviceRealTime(pageNo, pageSize, secret, partnerCode);

            if (response != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode())) {

                    if (response.getBody() != null) {

                        Integer deviceNum = response.getBody().getDeviceNum();

                        List<ResponseBatchDeviceTradeData> trList = new ArrayList<ResponseBatchDeviceTradeData>();

                        if (response.getBody().getDevice() != null && response.getBody().getDeviceNum() != null && response.getBody().getPage() != null) {

                            Page page = null;

                            for (BatchDeviceRealTime device : response.getBody().getDevice()) {

                                if (device.getPackage_order() != null) {
                                    ResponseBatchDevicePackageOrder or = null;

                                    for (BatchDeviceRealTimePackageOrder order : device.getPackage_order()) {

                                        List<ResponseBatchDeviceOrderRefuelingPackage> reList = new ArrayList<ResponseBatchDeviceOrderRefuelingPackage>();

                                        if (StringUtils.isNotBlank(order.getIsOrderRefuelingPackage()) && order.getIsOrderRefuelingPackage().equals("1") && order.getRefueling_package_list() != null) {
                                            //1，有加油包
                                            for (BatchDeviceRealTimeRefuelingPackageList re : order.getRefueling_package_list()) {
                                                ResponseBatchDeviceOrderRefuelingPackage refueling = new ResponseBatchDeviceOrderRefuelingPackage();

                                                refueling.setRefOrderId(re.getRefOrderId());
                                                refueling.setRefPackageFlow(re.getRefPackageFlow());
                                                refueling.setRefpackageOrderDate(re.getRefpackageOrderDate());
                                                refueling.setRefpackageStatus(re.getRefpackageStatus());
                                                reList.add(refueling);
                                            }
                                        }
                                        //2，无加油包
                                        or = new ResponseBatchDevicePackageOrder();
                                        or.setOrderId(order.getOrderCode());
                                        or.setPackageName(order.getPackageName());
                                        or.setPackageCode(order.getPackageCode());
                                        or.setPackageType(order.getPackageType());
                                        or.setBuyType(order.getBuyType());
                                        or.setOrderPeriod(order.getOrderPeriod());
                                        or.setFlow(order.getFlow());
                                        or.setUsedFlow(order.getUsedFlow());
                                        or.setFlowUseRate(order.getFlowUseRate());
                                        or.setOrderUsedFlow(order.getOrderUsedFlow());
                                        or.setOrderDate(order.getOrderDate());
                                        or.setActiveDate(order.getActiveDate());
                                        or.setExpireDate(order.getExpireDate());
                                        or.setIsOrderRefuelingPackage(order.getIsOrderRefuelingPackage());
                                        or.setRefuelingPackage(reList);

                                    }

                                    //设备信息
                                    ResponseBatchDeviceTradeData de = new ResponseBatchDeviceTradeData();
                                    de.setDeviceId(device.getDeviceId());
                                    de.setBipFlag(device.getBipFlag());
                                    de.setOtaFlag(device.getOtaFlag());
                                    de.setLifecycle(device.getLifecycle());
                                    de.setMultiImsiFlag(device.getMultiImsiFlag());
                                    de.setLifecycleStartTime(device.getLifecycleStartTime());
                                    de.setLifecycleEndTime(device.getLifecycleEndTime());
                                    de.setLifecycleSlientPeriod(device.getLifecycleSlientPeriod());
                                    de.setLifecycleShutdownPeriod(device.getLifecycleShutdownPeriod());
                                    de.setPackageOrder(or);
                                    trList.add(de);


                                }

                            }

                            page = new Page();
                            page.setCurrentPageNo(response.getBody().getPage().getCurrentPageNo());
                            page.setPageSize(response.getBody().getPage().getPageSize());
                            page.setTotalCount(response.getBody().getPage().getTotalCount());
                            page.setTotalPages(response.getBody().getPage().getTotalPages());


                            ResponseBatchDeviceRealTime res = new ResponseBatchDeviceRealTime();
                            res.setTradeData(trList);
                            res.setPage(page);
                            res.setDeviceNum(deviceNum);
                            res.setRetCode(response.getRetCode());
                            res.setResultInfo(response.getResultInfo());
                            // 返回给调用者的报文
                            jsonString = JSON.toJSONString(res);
                            logger.info("【批量获取拥有的设备信息】成功响应：" + jsonString);
                            return jsonString;

                        } else {
                            ResponseBatchDeviceRealTime responseOrder = new ResponseBatchDeviceRealTime();
                            responseOrder.setRetCode("9998");
                            responseOrder.setResultInfo("响应device为空");
                            // 返回给调用者的报文
                            jsonString = JSON.toJSONString(responseOrder);
                            logger.info("【批量获取拥有的设备信息】响应：" + jsonString);
                            return jsonString;
                        }


                    } else {

                        ResponseBatchDeviceRealTime responseOrder = new ResponseBatchDeviceRealTime();
                        responseOrder.setRetCode("9998");
                        responseOrder.setResultInfo("响应body为空");
                        // 返回给调用者的报文
                        jsonString = JSON.toJSONString(responseOrder);
                        logger.info("【批量获取拥有的设备信息】响应：" + jsonString);
                        return jsonString;

                    }

                } else {

                    ResponseBatchDeviceRealTime responseOrder = new ResponseBatchDeviceRealTime();
                    responseOrder.setRetCode(response.getRetCode());
                    responseOrder.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【批量获取拥有的设备信息】响应：" + jsonString);
                    return jsonString;

                }

            } else {

                ResponseBatchDeviceRealTime responseOrder = new ResponseBatchDeviceRealTime();
                responseOrder.setRetCode("9998");
                responseOrder.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【批量获取拥有的设备信息】：" + jsonString);
                return jsonString;

            }


        } else {

            ResponseBatchDeviceRealTime responseOrder = new ResponseBatchDeviceRealTime();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【批量获取拥有的设备信息】响应：" + jsonString);
            return jsonString;
        }

    }

}
